clear *

use "${root}/data/processed/final_sample.dta", clear

keep if baseline_sample == 1

rdrobust res_social_exp_avg_share margin_mayor_left, vce(cluster mun_code) all
local coeff_avg : di %9.2f (e(tau_bc))
local se_avg : di %9.2f (e(se_tau_rb))

foreach year in "3minus" "2minus" "1minus" "0"{	
	rdrobust res_social_exp_`year'_share margin_mayor_left if placebo_lagged_sample == 1, vce(cluster mun_code) all
	local coeff_`year' : di %9.2f (e(tau_bc))
	local se_`year' : di %9.2f (e(se_tau_rb))
	
}

foreach year in "1st" "2nd" "3rd" "4th"{	
	rdrobust res_social_exp_`year'_share margin_mayor_left, vce(cluster mun_code) all
	local coeff_`year' : di %9.2f (e(tau_bc))
	local se_`year' : di %9.2f (e(se_tau_rb))
	
}

mat baseline = (`coeff_3minus', `se_3minus' \ `coeff_2minus', `se_2minus' \ `coeff_1minus', `se_1minus' \ `coeff_0', `se_0' \ `coeff_1st', `se_1st' \ `coeff_2nd', `se_2nd' \ `coeff_3rd', `se_3rd' \ `coeff_4th', `se_4th' \ `coeff_avg', `se_avg')

mat list baseline

mat baseline_t = baseline'
mat list baseline_t
matrix colnames baseline_t = "-4" "-3" "-2" "-1 (election)" "1st" "2nd" "3rd" "4th" "Term average"

rdrobust res_social_exp_avg_share margin_mayor_left if oil_sample==1, vce(cluster mun_code) all
local oil_coeff_avg : di %9.2f (e(tau_bc))
local oil_se_avg : di %9.2f (e(se_tau_rb))

foreach year in "3minus" "2minus" "1minus" "0"{	
	rdrobust res_social_exp_`year'_share margin_mayor_left if (placebo_lagged_sample == 1 & oil_sample == 1), vce(cluster mun_code) all
	local oil_coeff_`year' : di %9.2f (e(tau_bc))
	local oil_se_`year' : di %9.2f (e(se_tau_rb))
}

foreach year in "1st" "2nd" "3rd" "4th"{	
	rdrobust res_social_exp_`year'_share margin_mayor_left if oil_sample == 1, vce(cluster mun_code) all
	local oil_coeff_`year' : di %9.2f (e(tau_bc))
	local oil_se_`year' : di %9.2f (e(se_tau_rb))
}
	

mat oil_sample = (`oil_coeff_3minus', `oil_se_3minus' \ `oil_coeff_2minus', `oil_se_2minus' \ `oil_coeff_1minus', `oil_se_1minus' \ `oil_coeff_0', `oil_se_0' \ `oil_coeff_1st', `oil_se_1st' \ `oil_coeff_2nd', `oil_se_2nd' \ `oil_coeff_3rd', `oil_se_3rd' \ `oil_coeff_4th', `oil_se_4th' \ `oil_coeff_avg', `oil_se_avg')

mat oil_sample_t = oil_sample'
mat list oil_sample_t
matrix colnames oil_sample_t = "-4" "-3" "-2" "-1 (election)" "1st" "2nd" "3rd" "4th" "Term average"


rdrobust res_social_exp_avg_share margin_mayor_left if lame_duck==1, vce(cluster mun_code) all
local lame_coeff_avg : di %9.2f (e(tau_bc))
local lame_se_avg : di %9.2f (e(se_tau_rb))	

foreach year in "3minus" "2minus" "1minus" "0"{	
	rdrobust res_social_exp_`year'_share margin_mayor_left if (placebo_lagged_sample == 1 & lame_duck==1), vce(cluster mun_code) all
	local lame_coeff_`year' : di %9.2f (e(tau_bc))
	local lame_se_`year' : di %9.2f (e(se_tau_rb))
}

foreach year in "1st" "2nd" "3rd" "4th"{	
	rdrobust res_social_exp_`year'_share margin_mayor_left if lame_duck == 1, vce(cluster mun_code) all
	local lame_coeff_`year' : di %9.2f (e(tau_bc))
	local lame_se_`year' : di %9.2f (e(se_tau_rb))
}

mat lame_duck = (`lame_coeff_3minus', `lame_se_3minus' \ `lame_coeff_2minus', `lame_se_2minus' \ `lame_coeff_1minus', `lame_se_1minus' \ `lame_coeff_0', `lame_se_0' \ `lame_coeff_1st', `lame_se_1st' \ `lame_coeff_2nd', `lame_se_2nd' \ `lame_coeff_3rd', `lame_se_3rd' \ `lame_coeff_4th', `lame_se_4th' \ `lame_coeff_avg', `lame_se_avg')

mat lame_duck_t = lame_duck'
mat list lame_duck_t
matrix colnames lame_duck_t = "-4" "-3" "-2" "-1 (election)" "1st" "2nd" "3rd" "4th" "Term average"


coefplot (matrix(baseline_t), se(2) color(black) ciopts(color(black) recast(rcap)) msymbol(oh) label("Baseline")) ///
	(matrix(lame_duck_t),se(2) color(blue) ciopts(color(blue) recast(rcap)) msymbol(dh) label("Lame ducks subsample")) ///
	(matrix(oil_sample_t), se(2) color(gray) ciopts(color(gray) recast(rcap)) msymbol(th) label("Oil windfall subsample")), ///
	vertical xtitle("Year in Office") xlabel(1 "-4" 2 "-3" 3 "-2" 4 `" "-1" "(Election)" "' 5 "1st" 6 "2nd" 7 "3rd" 8 "4th" 9 `" "Term" "average" "') ylabel(-4(2)8) ytitle("Social expenditure share (p.p.)") yline(0, lpattern(dash)) xline(8.5, lcolor(ebg) lwidth(thick) lpattern(longdash)) xline(4.5, lcolor(ebg) lwidth(thick) lpattern(longdash)) scheme(plotplainblind) legend(rows(1) region(lwidth(none)) position(6)) /* ysize(4) xsize(6) */ name(all)
graph export "${root}/results/figures/social_exp_dynamics.pdf", name("all") replace

